﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Dialogs.Environment
{
    /// <summary>
    /// Interaction logic for AddEnvironmentDialog.xaml
    /// </summary>
    public partial class AddEnvironmentDialog : Window
    {
        private Data.Environment.Environment _newEnvironment = null;
        private ObservableSortedList<Data.Environment.Environment> existingEnvironments = null;
        private bool _passedValidation = false;

        public AddEnvironmentDialog()
        {
            InitializeComponent();
        }

        public AddEnvironmentDialog(ObservableSortedList<Data.Environment.Environment> existingData)
        {
            InitializeComponent();
            existingEnvironments = existingData;
        }

        public Data.Environment.Environment newEnvironment 
        {
            get
            {
                if (_newEnvironment == null || _newEnvironment.Name != this.newEnvironmentControl.NewEnvironmentName)
                {
                    _newEnvironment = new Data.Environment.Environment();
                    _newEnvironment.Name = newEnvironmentControl.NewEnvironmentName;
                }
                return _newEnvironment;
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {

            if (existingEnvironments != null)
            {
                _passedValidation = true;
                foreach (Data.Environment.Environment env in existingEnvironments)
                {
                    if (string.Equals(env.Name, newEnvironmentControl.NewEnvironmentName, StringComparison.CurrentCulture))
                    {
                        _passedValidation = false;
                        break;
                    }
                }
            }
            else
            {
                _passedValidation = true;
            }
            DialogResult = true;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!_passedValidation && DialogResult == true)
            {
                MessageBox.Show("Cannot create duplicate Environment.", "Duplication Environment");
                e.Cancel = true;
            }
            if (DialogResult == true && string.IsNullOrEmpty(newEnvironmentControl.NewEnvironmentName))
            {
                MessageBox.Show("Environment name must have a value", "No name");
                e.Cancel = true;
            }
        }
    }
}
